package com.beaven.imclient.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.IntDef;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.base.BaseHideActivity;
import com.beaven.imclient.fragment.FollowFragment;
import com.beaven.imclient.fragment.GroupFragment;
import com.beaven.imclient.fragment.GroupUpdateFragment;
import com.beaven.imclient.fragment.NickFragment;
import com.beaven.imclient.fragment.PwdFragment;
import com.beaven.imclient.utils.ActivityUtil;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class SettingActivity extends BaseHideActivity {

  public static final String TYPE_SETTING = "SETTING_TYPE";
  // 昵称修改
  public static final int TYPE_NAME = 0x001;
  // 密码修改
  public static final int TYPE_PWD = 0x002;
  // 好友添加
  public static final int TYPE_SEARCH_NAME = 0x003;
  // 群组创建
  public static final int TYPE_CREATE_GROUP = 0x004;
  // 群组昵称更新
  private static final int TYPE_UPDATE_GROUP = 0x005;

  @IntDef(value = { TYPE_NAME, TYPE_PWD, TYPE_SEARCH_NAME, TYPE_CREATE_GROUP })
  @Retention(RetentionPolicy.SOURCE) @interface SettingType {
  }

  public static void start(Context context, @SettingType int type) {
    Intent intent = new Intent(context, SettingActivity.class);
    intent.putExtra(TYPE_SETTING, type);
    context.startActivity(intent);
  }

  public static void startGroupUpdate(Context context, String groupId) {
    Intent intent = new Intent(context, SettingActivity.class);
    intent.putExtra(TYPE_SETTING, TYPE_UPDATE_GROUP);
    intent.putExtra(ChatActivity.KEY_RECEIVER_ID, groupId);
    context.startActivity(intent);
  }

  private Fragment currentFragment;
  private String content;

  @Override public int getLayoutId() {
    return R.layout.activity_setting;
  }

  @Override protected void initBefore() {
    super.initBefore();
    int currentType = getIntent().getIntExtra(TYPE_SETTING, 0);
    switch (currentType) {
      case TYPE_NAME:
        content = getString(R.string.change_nick);
        currentFragment = NickFragment.newInstance();
        break;
      case TYPE_PWD:
        content = getString(R.string.change_password);
        currentFragment = PwdFragment.newInstance();
        break;
      case TYPE_SEARCH_NAME:
        content = getString(R.string.add_friend);
        currentFragment = FollowFragment.newInstance();
        break;
      case TYPE_CREATE_GROUP:
        content = getString(R.string.create_group);
        currentFragment = GroupFragment.newInstance();
        break;
      case TYPE_UPDATE_GROUP:
        content = getString(R.string.update_group);
        currentFragment = GroupUpdateFragment.newInstance();
        break;
      default:
        break;
    }
  }

  @Override protected void initWidget() {
    super.initWidget();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
    toolbar.setNavigationIcon(R.drawable.image_back_left);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        finish();
      }
    });
    View view_status_bar = findViewById(R.id.view_status_bar);
    view_status_bar.setBackgroundColor(getResources().getColor(R.color.primary));
    TextView text_content_center = (TextView) findViewById(R.id.text_content_center);
    text_content_center.setText(content);

    ActivityUtil.replaceFragmentToActivity(getSupportFragmentManager(), currentFragment,
        R.id.layout_setting_frame);
  }
}
